package cn.bdqn.pn.service.base.impl;
import cn.bdqn.pn.pojo.base.Coupon;
import cn.bdqn.pn.reposity.base.CouponReposity;
import cn.bdqn.pn.service.base.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class CouponServiceImpl implements CouponService{
    @Autowired
    private CouponReposity couponReposity;

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @Override
    public Page<Coupon> getAllCouponList(Coupon coupon, @RequestParam(required = false)Integer pageNo, Integer size) {

        Specification<Coupon> specification=new Specification<Coupon>() {
            @Override
            public Predicate toPredicate(Root<Coupon> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicates =new ArrayList<Predicate>();
                if(coupon.getCouponid()!=null && !coupon.getCouponid().equals("")){
                    predicates.add(cb.equal(root.get("cityoneId"),coupon.getCouponid()));
                }
                if(coupon.getCouponname()!=null && !coupon.getCouponname().equals("")){
                    System.out.println(coupon.getCouponname()+"+++++++++++++++++++++++++++++++++++++");
                    predicates.add(cb.like(root.get("couponname"),"%"+coupon.getCouponname()+"%"));
                }
                if(coupon.getCoupontype()!=null && !coupon.getCoupontype().equals("")){
                    predicates.add(cb.equal(root.get("coupontype"),coupon.getCoupontype()));
                }
                if(coupon.getStatus()!=null && !coupon.getStatus().equals("")){
                    predicates.add(cb.equal(root.get("status"),coupon.getStatus()));
                }
                if(coupon.getDisable()!=null && !coupon.getDisable().equals("")){
                    predicates.add(cb.equal(root.get("disable"),coupon.getDisable()));
                }
                Predicate [] predicates1=new Predicate[predicates.size()];
                return cb.and(predicates.toArray(predicates1));
            }
        };
        Pageable pageable=new PageRequest(pageNo==null?0:pageNo,size==null?6:size);
        return couponReposity.findAll(specification,pageable);
    }

    /**
     * 添加操作
     *
     * @param coupon
     * @return
     */
    @Override
    public Coupon addCoupon(Coupon coupon) {
        return couponReposity.save(coupon);
    }

    /**
     * 删除操作
     *
     * @param coupon
     * @return
     */
    @Override
    public Coupon updateCoupon(Coupon coupon) {
        return couponReposity.save(coupon);
    }

    /***
     * id查询
     * @param
     */
    @Override
    public Coupon findaddById(Integer id) {
        return couponReposity.getOne(id);
    }
}
